package org.apache.zookeeper.inspector.gui.nodeviewer;


import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class NodeACLManagerDialog extends JDialog implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4042723044567107424L;
	
	private JPanel panel;
	private JCheckBox read;
	private JCheckBox write;
	private JCheckBox create;
	private JCheckBox delete;
	private JCheckBox admin;
	private JCheckBox all;
	private JLabel aclSelect;
	private List<JCheckBox> aclList;
	
	@SuppressWarnings("unused")
	private JRadioButton digest;
	@SuppressWarnings("unused")
	private JRadioButton ip;
	@SuppressWarnings("unused")
	private JLabel usernameLabel;
	@SuppressWarnings("unused")
	private JTextField usernameText;
	@SuppressWarnings("unused")
	private JTextField passwordText;
	@SuppressWarnings("unused")
	private JLabel passwordLabel;
	@SuppressWarnings("unused")
	private JPanel aclPanel;
	
	public NodeACLManagerDialog(Component c , String title) {
		this.setLocationRelativeTo(c);
		this.setTitle("节点权限增加");
		this.setSize(500,400);
		init();
		initComponent();
		this.add(panel);
		this.setVisible(true);
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	}
	
	@SuppressWarnings("unused")
	private void initComponent() {
		int w = panel.getWidth();
		int h = panel.getHeight();
		int x = 10;
		int y = 10;
		int height = 24;
		int width = 80;
		aclSelect.setBounds(x, y, 100, height);
		all.setBounds(x + 100, y, 100, height);
		read.setBounds(x, 2 * y + height, width, height);
		write.setBounds(x + width, 2 *y + height, width, height);
		create.setBounds(x + 2 * width, 2 *y + height, width, height);
		delete.setBounds(x + 3* width, 2 *y + height, width, height);
		admin.setBounds(x + 4 * width, 2 *y + height, width, height);
		
	}

	private void init() {
		panel = new JPanel();
		aclList = new ArrayList<JCheckBox>();
		panel.setSize(getWidth(), getHeight());
		panel.setLayout(null);
		read = new JCheckBox("READ");
		aclList.add(read);
		read.addActionListener(this);
		write = new JCheckBox("WRITE");
		aclList.add(write);
		write.addActionListener(this);
		create = new JCheckBox("CREATE");
		aclList.add(create);
		create.addActionListener(this);
		delete = new JCheckBox("DELETE");
		aclList.add(delete);
		delete.addActionListener(this);
		admin = new JCheckBox("ADMIN");
		aclList.add(admin);
		admin.addActionListener(this);
		all = new JCheckBox("全选");
		all.addActionListener(this);
		aclSelect = new JLabel("选择授予的权限:");
		panel.add(aclSelect);
		panel.add(read);
		panel.add(admin);
		panel.add(write);
		panel.add(create);
		panel.add(delete);
		panel.add(all);
		digest = new JRadioButton("用户名/密码");
		ip = new JRadioButton("IP鉴权");
	}
	
	public static void main(String[] args) {
		new NodeACLManagerDialog(null,null);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == all) {
			if(all.isSelected()) {
				for(JCheckBox box : aclList) {
					box.setSelected(true);
				}
			} else {
				for(JCheckBox box : aclList) {
					box.setSelected(false);
				}
			}
		}
		for(JCheckBox box : aclList) {
			if(e.getSource() == box) {
				if(!box.isSelected()) {
					all.setSelected(false);
				}
			}
		}
		
	}

}
